package com.biu.billiards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biu.billiards.mapper.OrderMapper;
import com.biu.billiards.model.bo.MtBO;
import com.biu.billiards.model.bo.WithdrawBO;
import com.biu.billiards.model.entity.Mt;
import com.biu.billiards.mapper.MtMapper;
import com.biu.billiards.model.entity.Withdraw;
import com.biu.billiards.model.vo.mt.AddMtVo;
import com.biu.billiards.model.vo.mt.GetMtVo;
import com.biu.billiards.model.vo.mt.UpdateMtVO;
import com.biu.billiards.service.IMtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biu.billiards.utils.MtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-12-23
 */
@Service
public class MtServiceImpl extends ServiceImpl<MtMapper, Mt> implements IMtService {

    @Resource
    private MtMapper mtmMapper ;

    @Override
    public IPage<MtBO> getMtPage(GetMtVo getMtVo) {
        QueryWrapper<Mt> query = new QueryWrapper<>();

        //新建分页条件
        Page<Mt> p = new Page<>(getMtVo.getCurrentPage(),getMtVo.getPageSize());
        IPage<Mt> userIPage = mtmMapper.selectPage(p,query);
        IPage<MtBO> page = new Page<>();
        List<MtBO> adminDetailBOList = new ArrayList<>();
        for (Mt mt: userIPage.getRecords()){
            MtBO adminDetailBO = new MtBO();
            BeanUtils.copyProperties(mt,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());

        return page;
    }

    @Override
    public Boolean updateMt(UpdateMtVO updateMtVO) {
        QueryWrapper<Mt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mt_id",updateMtVO.getMtId());

        Mt withdraw = mtmMapper.selectOne(queryWrapper);

        BeanUtils.copyProperties(updateMtVO,withdraw);


        return mtmMapper.update(withdraw, queryWrapper) == 1;
    }

    @Override
    public Mt selectShopIdMt(Integer shopId) {
        QueryWrapper<Mt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",shopId);

        return mtmMapper.selectOne(queryWrapper);
    }

    @Override
    public Mt selectMt(Integer mtId) {
        QueryWrapper<Mt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mt_id",mtId);
       return mtmMapper.selectOne(queryWrapper);

    }

    @Override
    public Boolean addMt(AddMtVo addMtVo) {
        Mt withdraw = new Mt();
        BeanUtils.copyProperties(addMtVo,withdraw);

        return mtmMapper.insert(withdraw) == 1;
    }

    @Override
    public Boolean deleteMt(Integer mtId) {
        QueryWrapper<Mt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mt_id",mtId);
        return mtmMapper.delete(queryWrapper) == 1;
    }

    @Override
    public Boolean mtAuth(GetMtVo getMtVo) {

       // 2a057254bfe4d26e9a4f4caf70eab0bc647b3c0d

        Mt mt = selectMt(getMtVo.getMtId());
        getMtVo.setMtKey(mt.getMtKey());
        getMtVo.setMtSecret(mt.getMtSecret());
        getMtVo.setAuthCode(getMtVo.getAuthCode());

        System.out.println("getMtVoasdasf");
//        System.out.println(getMtVo);
//        MtUtils.getAuth(getMtVo);
        System.out.println("aaaaxx");

        MtBO mtBO = MtUtils.getTokenOrBid(getMtVo);

        System.out.println("mtBO12");
        System.out.println(mtBO);

        UpdateMtVO updateMtVO = new UpdateMtVO();

        BeanUtils.copyProperties(mtBO,updateMtVO);


        System.out.println("updateMtVOupdateMtVO");
        System.out.println(updateMtVO);

        updateMt(updateMtVO);


        return false;
    }



}
